package com.test.designPattern.ch32;

import com.test.designPattern.ch32.service.Service;

/**
 * @ClassName ServiceLocator
 * @Description 创建服务定位器。
 * @Author 维康
 * @Date 2018/7/2 17:17
 * @Version 1.0
 **/
public class ServiceLocator {
    private static Cache cache;
    static {
        cache = new Cache();
    }
    public static Service getService(String jndiName){

        Service service = cache.getService(jndiName);

        if(service != null){
            return service;
        }

        InitialContext context = new InitialContext();
        Service service1 = (Service)context.lookup(jndiName);
        cache.addService(service1);
        return service1;
    }
}
